package vmcs.boundary;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//
//

import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import vmcs.boundary.listener.ActivateCustomerPanelButtonListener;
import vmcs.boundary.listener.ActivateMachineryPanelButtonListener;
import vmcs.boundary.listener.ActivateMaintainerPanelButtonListener;
import vmcs.boundary.listener.BeginSimulationButtonListener;
import vmcs.boundary.listener.EndSimulationButtonListener;
import vmcs.controller.MainController;
import vmcs.controller.SimulationController;

public class SimulatorControlPanel extends Frame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final String title = "Simulation Control Panel";

    public static final char BEGIN = 0;
    public static final char END = 1;
    public static final char ACT_CUSTOMER = 2;
    public static final char ACT_MAINTAINER = 3;
    public static final char ACT_MACHINERY = 4;
    public static final char ACT_EXIT = 5;
    private MainController   mainCtrl;

    private static final String labels[] = {
			"Begin Simulation",
			"End Simulation",
			"Activate Customer Panel",
			"Activate Maintainer Panel",
			"Activate Machinery Panel"
                        //"Exit"
			};

    private Button buttons[] = new Button [labels.length];
    private Font titleFont = new Font ("Helvetica", Font.BOLD, 18);
    private Font buttonFont = new Font ("Helvetica", Font.BOLD, 14);

    private SimulationController    simulationCtrl;

    public SimulatorControlPanel (SimulationController cont) {
        super (title);

        simulationCtrl = cont;
        mainCtrl = simulationCtrl.getMainController();

        for (int i = 0; i < labels.length; i++) {
            createButton(i, cont);
        }

        setLayout(new GridLayout(0, 1));

        add (createPanelLabel());
        add (buttons[BEGIN]);
        add (buttons[ACT_CUSTOMER]);
        add (buttons[ACT_MAINTAINER]);
        add (buttons[ACT_MACHINERY]);
        add (buttons[END]);
        //add (buttons[ACT_EXIT]);

        //setSize(200, 200);
        pack();
        setLocation(100, 100);

        addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent e) {
                simulationCtrl.getMainController().closeDown();
            }
	});
    }

    public void display(){
      this.setVisible(true);
    }

    private Label createPanelLabel() {
        Label l = new Label (title);
        l.setBackground(Color.blue);
        l.setForeground(Color.white);
        l.setFont(titleFont);
        return (l);
    }

    private void createButton(int id, SimulationController cont) {
        Button b = new Button (labels[id]);
        b.setBackground(Color.white);
        b.setForeground(Color.blue);
        b.setFont(buttonFont);
        switch (id){
        case SimulatorControlPanel.BEGIN :
            BeginSimulationButtonListener bl;
            bl = new BeginSimulationButtonListener(simulationCtrl);
            b.addActionListener(bl);
            break;
        case SimulatorControlPanel.END :
            EndSimulationButtonListener  el;
            el = new EndSimulationButtonListener(mainCtrl);
            b.addActionListener(el);
            break;
        case SimulatorControlPanel.ACT_MAINTAINER :
            ActivateMaintainerPanelButtonListener   ml;
            ml = new ActivateMaintainerPanelButtonListener(simulationCtrl);
            b.addActionListener(ml);
            break;
        case SimulatorControlPanel.ACT_MACHINERY :
            ActivateMachineryPanelButtonListener   l;
            l = new ActivateMachineryPanelButtonListener(simulationCtrl);
            b.addActionListener(l);
            break;
        case SimulatorControlPanel.ACT_CUSTOMER :
            ActivateCustomerPanelButtonListener   cl;
            cl = new ActivateCustomerPanelButtonListener(simulationCtrl);
            b.addActionListener(cl);
            break;
        }
        buttons [id] = b;
    }

    public void setSimulationActive (boolean isOn) {
        buttons[BEGIN].setEnabled (!isOn);
        buttons[ACT_MAINTAINER].setEnabled (isOn);
        buttons[ACT_CUSTOMER].setEnabled(isOn);
        buttons[ACT_MACHINERY].setEnabled (isOn);
        buttons[END].setEnabled (isOn);
    }


    public void setButtonState (char id, boolean state) {
        buttons[id].setEnabled (state);
    }

    public void closeDown () {
        dispose ();
    }

    public void stop(){
      mainCtrl.closeDown();
    }

    public void setActive(char  cmp, boolean st){
      setButtonState(cmp, st);
    }
}